package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExceptionUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.html.EscapeUtil;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TApplicationInfoMapper;
import com.ruoyi.system.domain.TApplicationInfo;
import com.ruoyi.system.service.ITApplicationInfoService;
import com.ruoyi.common.core.text.Convert;

import javax.validation.ConstraintViolationException;
import javax.validation.Validator;

/**
 * 报考信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-13
 */
@Service
public class TApplicationInfoServiceImpl implements ITApplicationInfoService 
{
    @Autowired
    private TApplicationInfoMapper tApplicationInfoMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Autowired
    protected Validator validator;

    private static final Logger log = LoggerFactory.getLogger(TApplicationInfoServiceImpl.class);

    /**
     * 查询报考信息
     * 
     * @param applicationId 报考信息主键
     * @return 报考信息
     */
    @Override
    public TApplicationInfo selectTApplicationInfoByApplicationId(Long applicationId)
    {
        return tApplicationInfoMapper.selectTApplicationInfoByApplicationId(applicationId);
    }

    /**
     * 查询报考信息列表
     * 
     * @param tApplicationInfo 报考信息
     * @return 报考信息
     */
    @Override
    public List<TApplicationInfo> selectTApplicationInfoList(TApplicationInfo tApplicationInfo)
    {
        return tApplicationInfoMapper.selectTApplicationInfoList(tApplicationInfo);
    }

    /**
     * 新增报考信息
     * 
     * @param tApplicationInfo 报考信息
     * @return 结果
     */
    @Override
    public int insertTApplicationInfo(TApplicationInfo tApplicationInfo)
    {
        tApplicationInfo.setCreateTime(DateUtils.getNowDate());

        //新增判断是否为原始数据-joe
        // 查询 sys_config 表中的sys.account.controlRowData的信息
        SysConfig conValue = sysConfigMapper.selectConfigById(12L);
        tApplicationInfo.setControlRowData(conValue.getConfigValue());
        return tApplicationInfoMapper.insertTApplicationInfo(tApplicationInfo);
    }





    /**
     * 修改报考信息
     * 
     * @param tApplicationInfo 报考信息
     * @return 结果
     */
    @Override
    public int updateTApplicationInfo(TApplicationInfo tApplicationInfo)
    {
        tApplicationInfo.setUpdateTime(DateUtils.getNowDate());
        return tApplicationInfoMapper.updateTApplicationInfo(tApplicationInfo);
    }

    /**
     * 批量删除报考信息
     * 
     * @param applicationIds 需要删除的报考信息主键
     * @return 结果
     */
    @Override
    public int deleteTApplicationInfoByApplicationIds(String applicationIds)
    {
        return tApplicationInfoMapper.deleteTApplicationInfoByApplicationIds(Convert.toStrArray(applicationIds));
    }

    /**
     * 删除报考信息信息
     * 
     * @param applicationId 报考信息主键
     * @return 结果
     */
    @Override
    public int deleteTApplicationInfoByApplicationId(Long applicationId)
    {
        return tApplicationInfoMapper.deleteTApplicationInfoByApplicationId(applicationId);
    }

    @Override
    public String importData(List<TApplicationInfo> userList, Boolean isUpdateSupport, String operName,String years)
    {
        if (StringUtils.isNull(userList) || userList.isEmpty())
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TApplicationInfo user : userList)
        {
            try
            {
                if(StringUtils.isNotEmpty(years)){
                    user.setYears(years);
                }
                // 验证是否存在这个联系方式id
                if (user.getApplicationId() == null)
                {
                    BeanValidators.validateWithException(validator, user);
                    user.setUpdateBy(operName);
                    user.setUpdateTime(DateUtils.getNowDate());
                    user.setCreateBy(operName);
                    user.setCreateTime(DateUtils.getNowDate());
                    tApplicationInfoMapper.insertTApplicationInfo(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、准考证号 " + user.getExamId() + " 姓名 " + user.getName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, user);
                    user.setUpdateBy(operName);
                    user.setUpdateTime(DateUtils.getNowDate());
                    tApplicationInfoMapper.updateTApplicationInfo(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、准考证号 " + user.getExamId() + " 姓名 " + user.getName() +  " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、准考证号 " + user.getExamId() + " 姓名 " + user.getName() + " 已存在");
                }
            }
            catch (Exception e)
            {

            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    public String genSysData(){
        SysDictData sysDictData04 = sysDictDataMapper.selectDictDataById(32L);
        SysDictData sysDictData10 = sysDictDataMapper.selectDictDataById(33L);
        for (int i = 0; i < 30; i++) {
            sysDictData04.setDictCode(null);
            Long dictCode = Long.valueOf(sysDictData04.getDictValue())+100;
            sysDictData04.setDictValue(dictCode.toString());
            System.out.println("dictCode="+dictCode);
            String dictLabel = dictCode.toString().substring(0,4) + sysDictData04.getDictLabel().substring(4,8);
            sysDictData04.setDictLabel(dictLabel);
            sysDictData04.setDictSort(sysDictData10.getDictSort()+1);
            System.out.println("dictLabel="+dictLabel);
            sysDictDataMapper.insertDictData(sysDictData04);
            sysDictData10.setDictCode(null);
            Long dictCode10 = Long.valueOf(sysDictData10.getDictValue())+100;
            sysDictData10.setDictValue(dictCode10.toString());
            System.out.println("dictCode10="+dictCode10);
            String dictLabel10 = sysDictData10.getDictValue().substring(0,4) + sysDictData10.getDictLabel().substring(4,8);
            sysDictData10.setDictLabel(dictLabel10);
            sysDictData10.setDictSort(sysDictData04.getDictSort());
            sysDictData10.setDictSort(sysDictData10.getDictSort()+1);
            System.out.println("dictLabel10="+dictLabel10);
            sysDictDataMapper.insertDictData(sysDictData10);
        }
        return "success";

    }

}
